<template>
  <div class="blog-item">
    <div class="cover">
      <img :src="cover" v-if="cover">
      <img v-else src="../assets/pp.jpg">
    </div>
    <div class="content">
      <div class="title">
        <router-link :to="'/homePage/' + blogId" class="a-link" @click="updateBrowseCount">{{ title }}</router-link>
        <!-- 如果是推荐博客，添加动态火焰效果 -->
        <span v-if="isRecommended" class="fire">🔥</span>
      </div>
      <div class="summary">{{ summary }}</div>
      <div class="info">
        <span class="time">编辑时间: {{ time }}</span>
        <span class="writer"> 作者: <router-link to="/web-user" class="a-link"> {{ writer }}</router-link></span>
        <span class="categoryName">分类: <router-link to="/web-category" class="a-link">{{ categoryName }}</router-link></span>
        <span class="browse">🔥被浏览次数: {{ browse }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import axios from 'axios';
import { computed } from 'vue';

// 从父组件传入的 props
const props = defineProps({
  blogId: {type: String},
  cover: {type: String},
  title: {type: String},
  summary: {type: String},
  time: {type: String},
  writer: {type: String},
  categoryName: {type: String},
  browse: {type: String},
});

// 判断是否是推荐博客（这里假设推荐博客有特定标记或者条件）
const isRecommended = computed(() => {
  return props.browse > 1000;  // 假设浏览量大于1000的就是推荐博客
});

const jwtToken = localStorage.getItem('jwtToken');
const axiosInstance = axios.create({
  headers: {
    'Token': jwtToken, // 将JWT令牌放入请求头的token字段
  },
});

const updateBrowseCount = () => {
  axiosInstance.put(`http://localhost:8080/updatebrowse/${props.blogId}`)
      .then(response => {
        // 浏览次数更新成功后的逻辑
      })
      .catch(error => {
        console.error('更新失败', error);
      });
};
</script>

<style lang="scss">
.blog-item {
  display: flex;
  padding: 10px 0px;
  border-bottom: 1px solid #ddd;

  .cover {
    width: 110px;
    height: 110px;
    background: #ddd;
    display: flex;
    align-items: center;
    overflow: hidden;

    img {
      width: 100%;
    }
  }

  .content {
    flex: 1;
    margin-left: 10px;

    .title {
      color: #1890ff;
      font-size: 18px;
      font-weight: bold;
      position: relative;

      /* 动态火焰效果 */
      .fire {
        position: absolute;
        top: 0;
        right: -20px;
        font-size: 20px;
        animation: flame 1s infinite alternate;
      }

      &:hover {
        color: #ff6347;
        text-decoration: underline;
      }
    }

    .summary {
      word-break: break-all;
      margin-top: 6px;
      font-size: 14px;
      line-height: 22px;
      white-space: normal;
      color: #999aaa;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }

    .info {
      color: #5f6471;
      font-size: 14px;
      margin-top: 10px;
      display: flex;
      align-items: center;

      .categoryName {
        margin-right: 10px;
      }

      .writer {
        margin-right: 10px;
      }

      .time {
        margin-right: 10px;
        font-size: 13px;
      }

      .browse {
        margin-right: 10px;
        font-size: 15px;
        margin-left: auto;
        color: rgb(235, 51, 5);
      }
    }
  }
}

/* 动态火焰效果 */
@keyframes flame {
  0% {
    transform: scale(1) rotate(0deg);
    color: #ff6347;
  }
  50% {
    transform: scale(1.2) rotate(10deg);
    color: #ff4500;
  }
  100% {
    transform: scale(1) rotate(0deg);
    color: #ff6347;
  }
}
</style>
